home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr47 / wasm223.zip / TICKS.ASM < prev    next >
Assembly Source File  |  1993-05-04  |  4KB  |  144 lines

  1. ;*******************************;
  2. ; WASM Tick Timer               ;
  3. ; By Eric Tauck                 ;
  4. ;                               ;
  5. ; Defines:                      ;
  6. ;                               ;
  7. ;   TicSys  current system time ;
  8. ;   TicRes  reset a timer       ;
  9. ;   TicPas  return ticks passed ;
  10. ;   TicWai  pause for a time    ;
  11. ;*******************************;
  12.  
  13. ;--- get current time
  14.  
  15.         call    TicSys                          ;get current time
  16.         sub     bx, bx                          ;zero offset
  17.         mov     cx, TICK_TIMERS                 ;number of timers
  18.  
  19. ;--- initialize each timer
  20.  
  21. _ticks1 mov     [_ticks_times + bx], ax         ;save low word
  22.         mov     [_ticks_times + bx + 2], dx     ;save high word
  23.         add     bx, 4                           ;point to next entry
  24.         loop    _ticks1                         ;loop for each timer
  25.  
  26.         jmp     _ticks_end
  27.  
  28. ;--- equates
  29.  
  30. _TIMER_MAX1  EQU     0018H   ;ticks per day, high word
  31. _TIMER_MAX2  EQU     00B0H   ;ticks per day, low word
  32.  
  33. ;--- timer data
  34.  
  35. TICK_TIMERS     EQU     10              ;number of timers (at least one)
  36. _ticks_times    DS      TICK_TIMERS * 4 ;timer values
  37.  
  38. ;========================================
  39. ; Return the system time in ticks. Note:
  40. ; the time is read directly because using
  41. ; the BIOS resets the overflow flag
  42. ; (which keeps DOS from detecting a new
  43. ; day).
  44. ;
  45. ; Out: DX:AX= tick value.
  46.  
  47. TicSys  PROC    NEAR
  48.         push    ds
  49.         mov     ax, 40H         ;BIOS data segment
  50.         mov     ds, ax
  51.         mov     ax, [6CH]       ;load low word
  52.         mov     dx, [6EH]       ;load high word
  53.         pop     ds
  54.         ret
  55.         ENDP
  56.  
  57. ;========================================
  58. ; Zero (reset) a timer value.
  59. ;
  60. ; In: AL= timer number.
  61.  
  62. TicRes  PROC    NEAR
  63.         sub     ah, ah          ;zero high byte
  64.         shl     ax
  65.         shl     ax              ;index times four
  66.  
  67.         push    ax
  68.         call    TicSys          ;get current ticks
  69.         pop     bx
  70.  
  71.         mov     [_ticks_times + bx], ax         ;save low word
  72.         mov     [_ticks_times + bx + 2], dx     ;save high word
  73.         ret
  74.         ENDP
  75.  
  76. ;========================================
  77. ; Return the time passed.
  78. ;
  79. ; In: AL= timer number.
  80. ;
  81. ; Out: DX:AX= ticks passed.
  82.  
  83. TicPas  PROC    NEAR
  84.         push    si
  85.         sub     ah, ah          ;zero high byte
  86.         shl     ax
  87.         shl     ax              ;index times four
  88.  
  89.         push    ax
  90.         call    TicSys          ;get current time
  91.         pop     si
  92.  
  93.         cmp     dx, [_ticks_times + si + 2]     ;compare high word
  94.         ja      _tcpas2                         ;jump if no wrap
  95.         jb      _tcpas1                         ;jump if wrap
  96.         cmp     ax, [_ticks_times + si]         ;compare low word
  97.         jae     _tcpas2                         ;jump if no wrap
  98.  
  99. ;--- wrap
  100.  
  101. _tcpas1 mov     bx, _TIMER_MAX1                 ;ticks per day in BX:CX
  102.         mov     cx, _TIMER_MAX2                 ;
  103.         sub     cx, [_ticks_times + si]         ;wrap around around ticks
  104.         sbb     bx, [_ticks_times + si + 2]     ;
  105.         add     ax, cx                          ;add current ticks
  106.         adc     dx, bx                          ;
  107.         pop     si
  108.         ret
  109.  
  110. ;--- no wrap
  111.  
  112. _tcpas2 sub     ax, [_ticks_times + si]         ;calculate ticks passed
  113.         sbb     dx, [_ticks_times + si + 2]     ;
  114.         pop     si
  115.         ret
  116.         ENDP
  117.  
  118. ;========================================
  119. ; Wait for a period of time.
  120. ;
  121. ; In: AX= ticks to pause.
  122.  
  123. TicWai  PROC    NEAR
  124.         push    di
  125.         mov     di, ax
  126.  
  127.         push    WORD [_ticks_times]     ;
  128.         push    WORD [_ticks_times + 2] ;save timer 0
  129.  
  130.         sub     al, al                  ;timer zero
  131.         call    TicRes                  ;reset timer
  132. _tcwai1 sub     al, al                  ;timer zero
  133.         call    TicPas                  ;get time passed
  134.         cmp     ax, di                  ;check if timeout
  135.         jbe     _tcwai1                 ;loop back if not time yet
  136.  
  137.         pop     WORD [_ticks_times + 2] ;
  138.         pop     WORD [_ticks_times]     ;restore timer 0
  139.         pop     di
  140.         ret
  141.         ENDP
  142.  
  143. _ticks_end
  144.